package qdu.rg012.testvp;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class TestVp2 extends Activity {
    List<String> urllist;
    static List<Bitmap> imglist;
    Myhandler mh;
    static Myvp2adp myvp2adp;
    static ImageView img;
    /**
     * Viewpager2也是容器，改装自RecycleView
     * Viewpager2可以和Fragment TabView 等组件实现组合功能
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ly_testvp2);
        mh = new Myhandler();
        urllist = new ArrayList<String>();

        initdata();
        new Thread(){
            public void run(){
                downloaddata();
            }
        }.start();
        myvp2adp = new Myvp2adp();
        ((ViewPager2)findViewById(R.id.vp2_test)).setAdapter(myvp2adp);
    }

    class Myvp2adp extends RecyclerView.Adapter<MyviewHolder>{

        @NonNull
        @Override
        public MyviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemview = LayoutInflater.from(TestVp2.this).inflate(R.layout.ly_testvp3_item, parent, false);
            return new MyviewHolder(itemview);
        }

        @Override
        public void onBindViewHolder(@NonNull MyviewHolder holder, int position) {
            if(imglist != null)
//                holder.iv_testvp2.setBackgroundColor(
//                        datalist.get(position).toArgb()
//                );
                try {
                    holder.iv_testvp2.setImageBitmap(imglist.get(position));
                    //img = holder.iv_testvp2;
                    Log.i("mytag", "img :" + position + " put into view");
                }catch(IndexOutOfBoundsException e){
                    e.printStackTrace();
                }
        }

        @Override
        public int getItemCount() {
            if(imglist != null){
                return imglist.size();
            }else
                return 0;
        }
    }
    class MyviewHolder extends RecyclerView.ViewHolder{
        ImageView iv_testvp2;
        public MyviewHolder(@NonNull View itemView) {
            super(itemView);
            iv_testvp2 = itemView.findViewById(R.id.iv_vp2item);
        }
    }

    public void initdata(){
        urllist.add("http://cst.qdu.edu.cn/__local/5/A7/80/BDD1913BE9F1A57A83AD16EFB7D_1F4A4B82_14106.jpg");
        urllist.add("http://cst.qdu.edu.cn/__local/3/48/58/413564094115A2663828CFF96D6_146075C7_1AA7D.jpg");
        urllist.add("http://cst.qdu.edu.cn/__local/6/E3/3E/52A5F599B302D64FC27572FB9DC_6EDF827C_337F3.jpg");
        urllist.add("http://cst.qdu.edu.cn/__local/8/E5/4A/FF42AE1330AAAC06B377A071929_BB764EC4_9787F.png");
        Log.i("mytag", "init url finished....");
//        datalist.add(Color.valueOf(Color.RED));
//        datalist.add(Color.valueOf(Color.GREEN));
//        datalist.add(Color.valueOf(Color.YELLOW));
//        datalist.add(Color.valueOf(Color.BLUE));
    }
    public void downloaddata(){
        Bitmap tmp = null;
        List<Bitmap> localbmplist = new ArrayList<Bitmap>();
        for(int i = 0; i< urllist.size(); i++){
            tmp = downloadbyurl(urllist.get(i));
            if(tmp != null) {
                localbmplist.add(tmp);
                Log.i("mytag", "image downloading done....");
            }
        }
        Message msg = Message.obtain();
        msg.what = 1;
        msg.obj = localbmplist;
        mh.sendMessage(msg);
    }
    private Bitmap downloadbyurl(String url){
        Bitmap bmp = null;
        InputStream is = null;
        HttpURLConnection httpconn = null;

        try {
            URL urllink = new URL(url);
            httpconn = (HttpURLConnection)urllink.openConnection();
            httpconn.setDoInput(true);
            Log.i("mytag", "current url:" + url);
            httpconn.connect();
            Log.i("mytag", "http state:" + httpconn.getResponseCode());
            if( httpconn.getResponseCode() == HttpURLConnection.HTTP_OK ){

                is = httpconn.getInputStream();
                //BitmapFactory.
                bmp = BitmapFactory.decodeStream(is);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(httpconn != null){
                httpconn.disconnect();
            }
        }
        return bmp;
    }
    static class Myhandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    //imglist = new ArrayList<Bitmap>();
                    imglist = (ArrayList<Bitmap>)msg.obj;
                    Log.i("mytag", "data to UI thread doen.....");
                    Log.i("mytag", "imglist size:" + imglist.size());

                    //((ViewPager2)findViewById(R.id.vp2_test)).setAdapter(new Myvp2adp());
                    TestVp2.myvp2adp.notifyDataSetChanged();
                    break;
            }
        }
    }
}
